package com.byCreatetion.adabter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.byCreation.t_globe.R;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.squareup.picasso.Picasso;

import YouTubeApi.CallYouTubeApi;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class TglobeAppAdapter extends BaseAdapter {

	private Activity activity;
	private LayoutInflater inflater = null;
	// public ImageLoader imageLoader;

	ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();

	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;

	private int[] id_img = { R.drawable.app1, R.drawable.app2, R.drawable.app3,
			R.drawable.app4, R.drawable.app5, R.drawable.app6, R.drawable.app7,
			R.drawable.app8 };

	public TglobeAppAdapter(Activity a, ArrayList al) {

		activity = a;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		MyArrList = al;

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.app_icon)
				.showImageForEmptyUri(R.drawable.app_icon)
				.showImageOnFail(R.drawable.app_icon).delayBeforeLoading(100)
				.cacheInMemory().cacheOnDisc()
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(new RoundedBitmapDisplayer(0)).build();
		initImageLoader(activity.getApplicationContext());

	}

	private class ViewHolder {
		private TextView title, subscriberCount, updated, txt_newvdo,
				txt_title, txt_catecory;
		private ImageView grid_item_image;

	}

	@Override
	public int getCount() {
		return MyArrList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		View vi = convertView;
		final ViewHolder holder;

		if (convertView == null) {
			vi = inflater.inflate(R.layout.item_tglobeapp_gridview, null);
			holder = new ViewHolder();
			holder.grid_item_image = (ImageView) vi
					.findViewById(R.id.grid_item_image);

			vi.setTag(holder);

			// Picasso.with(activity)
			// .load(MyArrList.get(position).get("img").toString())
			// .placeholder(R.drawable.ic_launcher)
			// .into(holder.grid_item_image);
			//

		} else {

			holder = (ViewHolder) vi.getTag();
		}

		// =================================SetData===================================
		try {
			imageLoader.displayImage(MyArrList.get(position).get("img").toString(), holder.grid_item_image, options,animateFirstListener);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vi;
	}

	public void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.memoryCacheExtraOptions(100, 100)
				  .threadPoolSize(3) // default
	        .threadPriority(Thread.NORM_PRIORITY - 1) // default
	        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
	        .denyCacheImageMultipleSizesInMemory()
	        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
	        .memoryCacheSize(2 * 1024 * 1024)
	        .discCacheSize(50 * 1024 * 1024)
	        .discCacheFileCount(100)
	        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
	        .imageDownloader(new BaseImageDownloader(context)) // default
	        .imageDecoder(new BaseImageDecoder()) // default
	        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
	        .build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

	@SuppressWarnings("unused")
	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		final static List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				//boolean firstDisplay = !displayedImages.contains(imageUri);
				//if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				//}
			}
		}
	}

}